#delimit;

cd "C:\Users\ejm5\Dropbox\Participation_Compliance_Team\HCM experiment";#delimit;

log using "StrategyScience_testlog_eddy.smcl", replace;

import excel "Output Sept 29_with tax ID.xls", sheet("final") firstrow clear;
set more off;


mvdecode Q*, mv(-99=.a);



gen total=Q1 +Q2 +Q3+ Q4+ Q5+ Q6+ Q7 +Q8+ Q9+ Q10;


generate treatment=1 if treat_AC==1;
replace treatment=2 if treat_AD==1;
replace treatment=3 if treat_BC==1;
replace treatment=4 if treat_BD==1;



generate treat_author=1 if treatment<=2;
replace treat_author=0 if treatment>2;
label variable treat_author  "Who Advised Change";
label values treat_author treat_author;
label define treat_author 1 "Safety Experts" 0 "Business";

generate treat_purpose=1 if treatment==1|treatment==3;
replace treat_purpose=0 if treat_purpose==.;
label variable treat_purpose  "Impact of Change";
label values treat_purpose treat_purpose;
label define treat_purpose 1 "Good for Business" 0 "Safe for Labor";

generate treat_safety=1-treat_purpose;
label variable treat_safety  "Impact of Change";
label values treat_safety treat_safety;
label define treat_safety 0 "Good for Biz (T1)" 1 "Safety (T2)";

generate treat_business=1-treat_author;
label variable treat_business  "Who Advised Change";
label values treat_business treat_business;
label define treat_business 0 "Safety Experts (T3)" 1 "Business (T4)";


tab treat_author treat_purpose;

replace red_tape=.a if red_tape==-99;
gen red_tape2=5-red_tape;

generate labor_3=labor;
replace labor_3=3 if labor>=3;









/***********************************reshape*********************************/
#delimit;
generate respondent_id=_n;
save hcmc_experiment.dta, replace;
drop Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14;
sort respondent_id;
save hcmc_experiment_v2.dta, replace;

use hcmc_experiment.dta, clear;
stack  Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14, into(Q);
by _stack, sort: generate respondent_id=_n;
save stack_hcmc_experiment.dta, replace;

merge m:1 respondent_id using hcmc_experiment_v2.dta;
generate experiment=1 if _stack==1|_stack==2|_stack==3|_stack==4|_stack==8|_stack==9|_stack==10|_stack==11;
replace experiment=0 if _stack==5|_stack==6|_stack==7|_stack==13|_stack==14|_stack==12;
label variable experiment  "Experimental Question";
label values experiment experiment;
label define experiment 1 "VCCI" 0 "Placebo";



drop _merge;
sort Tax_ID;
merge m:1 Tax_ID using  20151009_hcmctax.dta;
drop if Q==.;

encode sector_plus, gen(sector_id);
drop sector_plus2;
gen sector_plus2=sector_plus;
replace sector_plus2="C13" if sector_plus=="C15";
replace sector_plus2="C26" if sector_plus=="C27";
replace sector_plus2="Service" if sector_plus2=="E"|sector_plus2=="H"|sector_plus2=="J";
encode sector_plus2, gen(sector_id2);


generate less_safe=1 if _stack==3|_stack==5|_stack==8|_stack==9;
replace less_safe=0 if less_safe !=1;
generate more_safe=1 if _stack==10|_stack==13|_stack==6;
replace more_safe=0 if more_safe==.;
generate neutral=1 if _stack==1| _stack==2| _stack==4|_stack==7|_stack==14|_stack==12;
replace neutral=0 if neutral !=1;

label variable less_safe  "Revisions Damage Safety";
label values less_safe less_safe;
label define less_safe 0 "No" 1 "Yes";

generate evaluation=1 if less_safe==1;
replace evaluation=2 if neutral==1;
replace evaluation=3 if more_safe==1;

label variable evaluation  "Expert Evaluation";
label values evaluation evaluation;
label define evaluation 1 "Less Safe" 2 "Neutral" 3 "More Safe";

replace Q=1 if Q==.a;
generate inter_RCTSafety=experiment*treat_safety;
lab var inter_RCTSafety "Interaction of RCT and Purpose treatment";


generate inter_RCTBiz=experiment*treat_business;
lab var inter_RCTBiz "Interaction of RCT and Business Endorsement";
save hcmc_experiment_v2.dta, replace;

/****************************************************************************************/

/*Chi Square Test*/
#delimit;
tab Q experiment, col chi;
tab Q experiment if evaluation==1, col chi;
tab Q experiment if evaluation==2, col chi;
tab Q experiment if evaluation==3, col chi;

#delimit;
generate Q100=Q*100;

#delimit;
graph bar (mean) Q100, over(experiment) bar(1, fcolor(gs4) lcolor(gs4))  bar(2, fcolor(maroon) lcolor(maroon)) blabel(bar, size(medsmall) color(black) position(outside) 
format(%3.2f)) ytitle("Share of Respondents Choosing Revised Clause (%)", size(medsmall) margin(medium))
note("n=1,554. 121 respondents each answering 14 clauses;" "Difference in selection rates (Chi2=0.1; p=0.002).", 
size(medium) position(12)) scheme(s1color);
graph save Figure2.gph, replace;
graph export "Figure2.pdf", as(pdf) replace;

#delimit;
graph bar (mean) Q100, over(experiment) over(evaluation) bar(1, fcolor(gs4) lcolor(gs4)) 
blabel(bar, size(medsmall) color(black) position(outside) format(%3.2f)) 
 ytitle("Share of Respondents Choosing Revised Clause (%)", 
 size(medsmall) margin(medium)) scheme(s1color)
 note("Tests of difference in selection rates: Less Safe (n=484; chi2=2; p=.157)" 
 "Neutral (n=726; chi2=47.2; p=.000)  More Safe (n=363; chi2=7.9; p=.005).", size(medsmall) position(12));
graph save Figure3.gph, replace;
graph export "Figure3.pdf", as(pdf) replace;

#delimit;
graph bar (mean) Q100, over(experiment) over(treat_safety)  bar(1, fcolor(gs4) lcolor(gs4)) 
blabel(bar, size(medsmall) color(black) position(outside) format(%3.2f)) 
ytitle("Share of Respondents Choosing Revised Clause (%)", size(medium) margin(medium))
title("By Purpose Experiment", size(medlarge)) scheme(s1color);
graph save Fig4_a.gph, replace;


#delimit;
graph bar (mean) Q100, over(experiment) over(treat_business)  bar(1, fcolor(gs4) lcolor(gs4)) 
blabel(bar, size(medsmall) color(black) position(outside) format(%3.2f)) 
ytitle("Share of Respondents Choosing Revised Clause (%)", size(medium) margin(medium))
title("By Endorsement Experiment", size(medlarge)) scheme(s1color);
graph save Fig4_b.gph, replace;

#delimit;

tab Q experiment if treat_safety==0, col chi;
tab Q experiment if treat_safety==1, col chi;
tab Q experiment if treat_business==0, col chi;
tab Q experiment if treat_business==1, col chi;

#delimit;
graph combine Fig4_a.gph Fig4_b.gph, scheme(s1color)  ycommon imargin(tiny)
note("Tests of difference in selection rates:" "T1 (n=770; chi2=.84; p=.359) T2 (n=924; chi2=10.5; p=.001)" 
"T3 (n=742; chi2=2.3; p=.132) T4 (n=952; chi2=7.3; p=.007).", size(medsmall) position(12));
graph save Figure4.gph, replace;
graph export "Figure4.pdf", as(pdf) replace;


/*Better Law*/
#delimit;
meqrlogit Q less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) replace;

#delimit;
meqrlogit Q experiment less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) replace;

meqrlogit Q experiment treat_safety treat_business less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) ;

xi: meqrlogit Q experiment treat_safety treat_business i.labor_size female  i.sector_plus2 less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) ;

meqrlogit Q experiment treat_safety inter_RCTSafety treat_business less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) ;

xi: meqrlogit Q experiment treat_safety inter_RCTSafety treat_business i.labor_size female  i.sector_plus2 less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2);

xi: meqrlogit Q experiment  treat_business  inter_RCTBiz treat_safety less_safe more_safe || _all: R._stack || respondent_id: , or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2);

#delimit;
xi: meqrlogit Q experiment treat_business inter_RCTBiz treat_safety  i.labor_size female  i.sector_plus2  less_safe more_safe || _all: R._stack || respondent_id:, or;
outreg2 using Table6, eform cti(odds ratio) tdec(3) bdec(3) e(N_clust pbar r2_p ll chi2) excel;

/*Calculating Marginal Probabilities - Estimates are the Same but Statistical Significance biased Upward*/
#delimit;
logit Q i.experiment treat_safety treat_business less_safe more_safe i.labor_size female  i.sector_id2 , or;
margins experiment;
margins, dydx(experiment);


#delimit;
logit Q experiment##treat_safety treat_business less_safe more_safe i.labor_size female  i.sector_id2 , or;
margins experiment##treat_safety;
margins, dydx(experiment) by(treat_safety);
/****************************************************************************************/
generate placebo=1-experiment;
xi: meqrlogit Q placebo treat_safety treat_business i.labor_size female  i.sector_plus2 less_safe more_safe || _all: R._stack || respondent_id:, or;



/*************************Individual Level***********************************************************/

#delimit;
use hcmc_experiment.dta, clear;
sort Tax_ID;
merge Tax_ID using  20151009_hcmctax.dta;
drop if _merge !=3;

impute red_tape2 treat_safety treat_business ln_employee ln_k  C16 C17 C20 C25 dis_1 firm_type_dummy1 firm_type_dummy2 , gen(red_tape3);
replace red_tape3=1 if red_tape3<=1.5;
replace red_tape3=2 if red_tape3>1.5 & red_tape3<=2.5;
replace red_tape3=3 if red_tape3>2.5 & red_tape3<=3.5;
replace red_tape3=4 if red_tape3>3.5;

#delimit;
generate red_tape_agree=1 if red_tape3>2 & red_tape3<=4;
replace red_tape_agree=0 if red_tape3<=2;
generate red_tape_100=red_tape_agree*100;

#delimit;
generate bribe_disagree=1 if red_tape==3|red_tape==4;
replace bribe_disagree=0 if bribe_disagree==.;

#delimit;
generate dk=100 if red_tape==.a;
replace dk=0 if red_tape!=.a;


/*************************************************************************************************/

#delimit;
cibar red_tape3, over1(treat_business)  level(95) barcolor(navy maroon) barlabel(on) blsize(small)  blposition(NE)
graphopts(scheme(s1color) ytitle("Regulations Used to Extract Bribes" "(1=S.Disagree; 4=S. Agree)", size(medium) margin(medium))  legend(rows(2) ring(0) position(1) size(small))
note("95% Confidence Intervals", size(vsmall)) xtitle("Clause Selection Criteria", size(medium) margin(medium)) ylab(1(.25)2.25)
title("Four-Point Coding", size(medium) margin(medium)));
graph save rents_2.gph, replace;


#delimit;
cibar red_tape3, over1(treat_business) over2(treat_safety) level(95) barcolor(navy maroon) barlabel(on) blsize(small)  blposition(NE)
graphopts(scheme(s1color) ytitle("Regulations Used to Extract Bribes" "(1=S.Disagree; 4=S. Agree)", size(medium) margin(medium))  legend(off) 
xtitle("Clause Selection Criteria", size(medium) margin(medium)) ylab(1(.25)2.25)
title("Four-Point Coding", size(medium) margin(medium)));
graph save rents_4.gph, replace;

graph combine rents_2.gph rents_4.gph,  imargin(tiny) scheme(s1color);

/*************************************************************************************************/

#delimit;
tab treat_business red_tape_100 , row chi;

#delimit;
graph bar (mean) red_tape_100, over(treat_business) bar(1, fcolor(gs4) lcolor(gs4)) 
blabel(bar, size(medsmall) color(black) position(outside) format(%3.2f)) 
 ytitle("Regulations Used to Extract Bribes(%)", 
 size(medsmall) margin(medium))
 note("Tests of difference in selection rates:" "n=121; chi2=8.6; p=.003", margin(medium) size(medium) position(12)) scheme(s1color);
graph save Figure6.gph, replace;
graph export "Figure6.pdf", as(pdf) replace;


/*************************************************************************************************/

#delimit;
tab treat_business dk , row chi;
graph bar (mean) dk, over(treat_business) bar(1, fcolor(gs4) lcolor(gs4)) 
blabel(bar, size(medsmall) color(black) position(outside) format(%3.2f)) 
 ytitle("Respondents Answering Don't Know(%)", 
 size(medsmall) margin(medium))
 note("Tests of difference in selection rates:" "n=121; chi2=.85; p=.356", margin(medium) size(medium) position(12)) scheme(s1color);
graph save Figure5.gph, replace;
graph export "Figure5.pdf", as(pdf) replace;



/*************************************************************************************************/

#delimit;
set more off;
reg red_tape3 treat_business treat_safety, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse ll chi2 ) replace ;
reg red_tape3   treat_business treat_safety i.labor_size female, robust; 
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
xi: reg red_tape3  treat_business treat_safety i.labor_size female  i.sector_plus2, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2) excel;
xi: oprobit red_tape3  treat_business treat_safety i.labor_size female  i.sector_plus2, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
xi: reg red_tape2  treat_business treat_safety i.labor_size female  i.sector_plus2, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2) excel;
xi: oprobit red_tape2  treat_business treat_safety i.labor_size female  i.sector_plus2, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);

#delimit;
reg red_tape_agree treat_business treat_safety, robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
reg red_tape_agree  treat_business treat_safety i.labor_size female , robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
xi: reg red_tape_agree  treat_business treat_safety i.labor_size female  i.sector_plus2 , robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2) excel;
xi: dprobit red_tape_agree  treat_business treat_safety i.labor_size female  i.sector_plus2 , robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
xi: reg red_tape_agree  treat_business treat_safety i.labor_size female  i.sector_plus2 if red_tape2 !=. , robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2);
xi: dprobit red_tape_agree  treat_business treat_safety i.labor_size female  i.sector_plus2 if red_tape2 !=., robust;
outreg2 using H1,  tdec(3) bdec(3) e(N_clust pbar r2_p rmse  ll chi2) excel;












#delimit;
replace legitimacy=legitimacy*100;
replace quality=quality*100;
replace knowledge=knowledge*100;

#delimit;
graph bar (mean) legitimacy (mean) quality (mean) knowledge, bar(1, fcolor(navy) lcolor(white)) bar(2, fcolor(dkgreen) lcolor(white)) 
bar(3, fcolor(maroon) lcolor(white)) blabel(bar, size(medsmall) color(white) position(center) orientation(horizontal) format(%2.1f)) bargap(5) 
	ytitle("Percentrage of Respondents Answering 'Yes' ", size(medium) margin(medium)) 
	legend(rows(1) position(6) size(small) label(1 "More legitimate") 
	label(2 "Higher quality") 
	label(3 "Better Understanding")) scheme(s1color);
graph save other_ques.gph, replace;


/***************************************BALANCE***************************************/

/*Red Tape*/
#delimit;
generate refuse=1 if red_tape==.a;
replace refuse=0 if red_tape !=.a;

#delimit;
encode sector_plus2, gen(sector_id2);
xi: collapse treat_AC treat_AD treat_BC treat_BD red_tape2 treat_safety treat_business labor legitimacy quality knowledge 
red_tape refuse labor_size labor_size2 total_capital capital_size ln_kl_ratio dis_1 
female firm_type_dummy1 firm_type_dummy2 firm_type_dummy3 firm_type_dummy4 C16 C17 C20 C25 
sector_id2 G ln_employees ln_k , by(respondent_id);


/*Treatment Balance*/
replace treat_AC=0 if treat_AC==.;
replace treat_AD=0 if treat_AD==.;
replace treat_BC=0 if treat_BC==.;
replace treat_BD=0 if treat_BD==.;

#delimit;
generate treatment=1 if treat_AC==1;
replace treatment=2 if treat_AD==1;
replace treatment=3 if treat_BC==1;
replace treatment=4 if treat_BD==1;




#delimit;
preserve;
set more off;
reg treat_safety;
outreg2 using balance, tdec(3) bdec(3) long replace;
foreach x in refuse labor_size  capital_size 
	dis_1 firm_type_dummy1 
	firm_type_dummy2 firm_type_dummy3 firm_type_dummy4 female 
	C16 C17 C20 C25 G ln_employees ln_k   {;
reg `x' treat_AC treat_AD treat_BC treat_BD, noconstant ;
outreg2 using balance, tdec(3) bdec(3) ci  noaster long;
reg `x' treat_AC treat_AD treat_BC treat_BD;
outreg2 using balance, tdec(3) bdec(3) ci  noaster long  e(F);
};
outreg2 using balance, tdec(3) bdec(3) long excel;


#delimit;
foreach x in refuse labor_size  capital_size 
	dis_1 firm_type_dummy1 
	firm_type_dummy2 firm_type_dummy3 firm_type_dummy4 female 
	C16 C17 C20 C25 G ln_employees ln_k   {;
kwallis `x', by(treatment);
scalar define chi_`x'= chi2tail(3,r(chi2));
};


#delimit;
foreach x in refuse labor_size  capital_size 
	dis_1 firm_type_dummy1 
	firm_type_dummy2 firm_type_dummy3 firm_type_dummy4 female 
	C16 C17 C20 C25 G ln_employees ln_k   {;
scalar list chi_`x';
};



/************************************************************************************************/

/*Scatter of Correlations between Safety and Business Goals*/
#delimit;
#delimit;
use hcmc_experiment_v2.dta, clear;
collapse Q experiment less more neutral, by(treat_safety _stack);
replace Q=Q*100;
xtset _stack treat_safety;
pwcorr Q l.Q;

#delimit;
twoway  (lfitci Q l.Q) 
	(scatter Q l.Q if experiment==1, mlabel(_stack) mlabsize(medium) msymbol(i) mlabcolor(navy)) 
	(scatter Q l.Q if experiment==0, mlabel(_stack) mlabsize(medium) msymbol(i) mlabcolor(maroon)), 
	xtitle("Safer for Labor %", size(medium) margin(medium)) ytitle("Better for Businesses %", 
	size(medium) margin(medium)) note("r=.92***", ring(0) position(5) size(medium))
	legend(off);
graph save q_scatter_final.gph, replace;


#delimit;
twoway  (lfitci Q l.Q) 
	(scatter Q l.Q if experiment==1 & less==1, mlabposition(3) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(square) mcolor(navy) mlabcolor(black)) 
	(scatter Q l.Q if experiment==0 & less==1, mlabposition(9) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(square) mcolor(maroon) mlabcolor(black))
	(scatter Q l.Q if experiment==1 & more==1, mlabposition(3) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(triangle) mcolor(navy) mlabcolor(black)) 
	(scatter Q l.Q if experiment==0 & more==1, mlabposition(9) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(triangle) mcolor(maroon) mlabcolor(black))
	(scatter Q l.Q if experiment==1 & neutral==1, mlabposition(3) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(diamond) mcolor(navy) mlabcolor(black)) 
	(scatter Q l.Q if experiment==0 & neutral==1, mlabposition(9) mlabel(_stack) mlabsize(medium) msize(medium) msymbol(diamond) mcolor(maroon) mlabcolor(black)), 
	xtitle("Firms Agreeing Safer for Labor % (T2)", size(medium) margin(medium)) ytitle("Firms Agreeing Better for Businesses % (T1)", 
	size(medium) margin(medium)) note("r=.92***", ring(0) position(11) size(medium))
	legend(rows(5) size(vsmall) position(5) ring(0) label(3 "Less Safe/VCCI") label(4 "Less Safe/Placebo") label(5 "More Safe/VCCI") label(6 "More Safe/Placebo")
	label(7 "Neutral/VCCI") label(8 "Neutral/Placebo")) scheme(s1color) ;
graph save q_scatter_final.gph, replace;


log close;
